package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyQingYuan;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 安心分期-佛山亿融金-单企业主20API
 *
 * @author WJB
 */
@ApiPlan(productId = "415", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskYiRongJin implements ApiOrgPhoneMaskConvertHandler {

    private static final String KEY = "hbt4j8ql7n7yjtfb";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        Map<Object, Object> newMap = new LinkedHashMap<>();
        AES aes = AES.createEcb(KEY.getBytes());
        newMap.put("phone", apiDataExchangeParameter.getPhoneMask());
        Map<Object, Object> data = new LinkedHashMap<>();
        data.put("data", aes.encryptToBase64Str(JsonUtils.toJson(newMap)));
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        JSONArray phoneList = jsonObject.getJSONArray("data");
        if (phoneList == null) {
            return RespOrgPhoneMaskDataExchange.ofSuc();
        }
        RespOrgPhoneMaskDataExchange respOrgPhoneMaskDataExchange = RespOrgPhoneMaskDataExchange.ofSuc();
        List<String> md5 = new ArrayList<>();
        for (int i = 0; i < phoneList.size(); i++) {
            String phoneMd5 = phoneList.getString(i);
            md5.add(phoneMd5);
        }
        respOrgPhoneMaskDataExchange.setListPhoneMd5(md5);
        return respOrgPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {

        OrgApplyQingYuan data = new OrgApplyQingYuan();
        data.setName(apiApplyParameter.getUserName());
        data.setPhone(apiApplyParameter.getPhone());
        data.setSource("安心分期");
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        data.setAge(apiFieldConverter.getAgeInt());
        //  性别 1：男 2：女 3：未知
        String sex = apiApplyParameter.getSex();
        String sexChoose = ChooseEq.create()
                .when(sex, "0", "1")
                .when(sex, "1", "2")
                .end("3");
        data.setGender(sexChoose);
        // 城市
        data.setCity(apiApplyParameter.getCity());
        // 职业
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
            String career = ChooseEq.create()
                    .when(apiApplyParameter.getCareer(), "1", "上班族")
                    .when(apiApplyParameter.getCareer(), "2", "自由职业")
                    .when(apiApplyParameter.getCareer(), "3", "企业主")
                    .when(apiApplyParameter.getCareer(), "4", "公务员或事业单位")
                    .when(apiApplyParameter.getCareer(), "5", "个体户")
                    .end();
            data.setProfession(career);
        }
        //  贷款额度 （单位：万）
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            String money = ChooseEq.create()
                    .when(apiApplyParameter.getLoanAmount(), "1", "3万")
                    .when(apiApplyParameter.getLoanAmount(), "2", "5万")
                    .when(apiApplyParameter.getLoanAmount(), "3", "10万")
                    .when(apiApplyParameter.getLoanAmount(), "4", "20万")
                    .end();
            data.setLoanAmount(money);
        }
        // 芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 4)
                .when(apiApplyParameter.getSesameCredit(), "2", 1)
                .when(apiApplyParameter.getSesameCredit(), "3", 2)
                .when(apiApplyParameter.getSesameCredit(), "4", 3)
                .end(5);
        data.setSesameSeed(zhima);
        //  社保
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            Integer shebao = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", 4)
                    .when(apiApplyParameter.getSocialSecurity(), "2", 2)
                    .when(apiApplyParameter.getSocialSecurity(), "3", 3)
                    .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                    .end();
            data.setSocialSecurity(shebao);
        }
        //  公积金
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            Integer gongjijin = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", 4)
                    .when(apiApplyParameter.getProvidentFund(), "2", 2)
                    .when(apiApplyParameter.getProvidentFund(), "3", 3)
                    .when(apiApplyParameter.getProvidentFund(), "4", 1)
                    .end();
            data.setProvidentFund(gongjijin);
        }
        //  房产情况
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            Integer house = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", 4)
                    .when(apiApplyParameter.getRealEstateInfo(), "2", 3)
                    .when(apiApplyParameter.getRealEstateInfo(), "3", 2)
                    .when(apiApplyParameter.getRealEstateInfo(), "4", 3)
                    .when(apiApplyParameter.getRealEstateInfo(), "5", 3)
                    .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                    .end();
            data.setHouse(house);
        }
        //  车
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            Integer car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", 4)
                    .when(apiApplyParameter.getCarInfo(), "2", 3)
                    .when(apiApplyParameter.getCarInfo(), "3", 2)
                    .when(apiApplyParameter.getCarInfo(), "4", 1)
                    .end();
            data.setVehicle(car);
        }
        //  保险
        data.setMain_zvqzwn(apiFieldConverter.hasInsurancePolicyToStr());
        //  营业执照
        if (ValidParam.isNotEmpty(apiApplyParameter.getBizLicenseYear())) {
            Integer bizLicenseYear = ChooseEq.create()
                    .when(apiApplyParameter.getBizLicenseYear(), "1", 2)
                    .end(1);
            data.setMain_uimmgv(bizLicenseYear);
        }

        Map<Object, Object> newMap = new LinkedHashMap<>();
        AES aes = AES.createEcb(KEY.getBytes());
        newMap.put("data", aes.encryptToBase64Str(JsonUtils.toJson(data)));
        return newMap;
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
